﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BoolanElement;
using BoolanSlide.FileStruct;
using Windows.Storage;

namespace BoolanSlide.Document.Storage
{
    public class ImageManager
    {
        public static async Task<StorageFile> AddImage(StorageFile imageFile)
        {
            String imageName = Guid.NewGuid().ToString() + imageFile.FileType;
            StorageFolder imageFolder = await BooUtility.GetFolderFromDocumentFolderAsync(BooDocumentHelper.ImageTmpFolder);
            StorageFile newFile = await imageFile.CopyAsync(imageFolder, imageName);
            return newFile;
        }

        public static async Task<StorageFile> GetImage(String imageName,StorageFolder baseFolder=null)
        {
            if (imageName == null || imageName == "")
                return null;
            StorageFile image = null;
            try
            {
                if (baseFolder == null)
                    baseFolder = BooDocumentFolderInformation.ImageResources;
                if (baseFolder != null)
                    image = await BooDocumentFolderInformation.ImageResources.GetFileAsync(imageName);
            }
            catch (Exception ex)
            {
                ex.ToString();
            }
            return image;
        }

        public static async Task<bool> CopyImage(StorageFolder src, StorageFolder des, string filename)
        {
            IReadOnlyList<StorageFile> list = await des.GetFilesAsync();
            bool isExist = false;
            foreach (var item in list)
            {
                if (item.Name == filename)
                {
                    isExist = true;
                    break;
                }
            }
            if (isExist)
                return false;
            await (await src.GetFileAsync(filename)).CopyAsync(des, filename);
            return true;
        }
    }
}
